<{capture name="header"}>
<{css app="ome" src="ome.css"}>
<link href="statics/singlepage.css" rel="stylesheet" type="text/css">
<{/capture}>

<div class="spage-main-box">
<div class="tableform">

<h3>发货处理 <span class="num">(目前还有<em class="count"><{$num}></em>个发货单未发货)</span></h3> 
<div class="division">
<form method="post" action="index.php?app=ome&ctl=admin_consign&act=consign" id="ome_single_delivery">
<input type="hidden" id="order_exists_refund" name="order_exists_refund" value="" />
<input type="hidden" id="delivery_id" name="delivery_id" />
<table width="100%" cellspacing="0" cellpadding="0" border="0">
  <tbody>
  <tr>
   <th></th>
    <td><span id="error" style="color:red; font-weight:bold; font-size:14px;display:none" class="error" ></span></td>
  </tr>
  <tr>
    <th>请输入快递单号：</th>
    <td><input type="text" id="logi_no" name="logi_no" vtype="required" /></td>
  </tr>
  <tr>
    <th>请输入货物总重量：</th>
    <td><input type="text" id="weight" name="weight" vtype="required&&unsignedint" /> 克</td>
  </tr>
</tbody>
</table>

<div class="table-action">
    <{button type="button" id="sendProduct" label="确定" }>
</div>

</form>
</div>
</div>

</div>
<script>
$('logi_no').focus();
$('logi_no').addEvent('keyup',function(e){
    if(e.code==13)$('weight').focus();
    return false;
});
$('weight').addEvent('keyup',function(e){
    if(e.code==13){
    delivery_verify(e);
    }
});
$('sendProduct').addEvent('click', function(e){
	delivery_verify(e);
});
function delivery_verify(e){
	var logi_no = $('logi_no').value;
    var weight = $('weight').value;
    if (logi_no == ''){
        $('error').show().set('html','请输入快递单号');return ;
    }
    $('delivery_id').set('value', logi_no);
    var delivery_id = encodeURIComponent(logi_no);
    new Request({url:'index.php?app=ome&ctl=admin_consign&act=batchCheck&delivery_type=single',data:'delivery_id='+delivery_id+'&weight='+weight,method:'POST',
      onRequest: function(){
          $('error').hide();
      },
      onComplete: function(json){
        if (json != ''){
            json = JSON.decode(json);
        }
        var refund_orders, is_confirm_delivery = false;
        if (json.status == 'error'){
            $('error').show().set('html',json.msg);
            //return MessageBox.error(json.msg);
        }else{
            if (json != ''){
	            json = json.filter(function(n){
	                if (n.order_exists_refund != ''){
	                	is_confirm_delivery = true;
	                    refund_orders = n.order_exists_refund;
	                }
	                return !n.order_exists_refund;
	            });
            }
            if(json != ''){
                var msg = "";
                json.each(function(j){
                    if (typeof(j.bn) != 'undefined'){
                        msg += j.bn + ' &nbsp; ' + j.msg + '<br />';
                    }
                });
                $('error').set('html',msg);
                return MessageBox.error('单号有异常');
            }else{
	            if (is_confirm_delivery == true && refund_orders != ''){
	            	$('order_exists_refund').set('value', refund_orders);
	                new Dialog('index.php?app=ome&ctl=admin_consign&act=batch_delivery_confirm&delivery_type=single',{title:'批量发货确认',width:500,height:300})
	            }else{
	                submitSend(e);
	            }
            }
        }
      }
    }).send();
}
function submitSend(e){
   $('ome_single_delivery').fireEvent('submit',{stop:$empty});
}

$('ome_single_delivery').store('target',{
    onRequest:function(){
       $('sendProduct').set('disabled', 'true');
       $('sendProduct').getElements('span')[1].set('text','正在发货');
    },
    onComplete:function(jsontext){
       var json = JSON.decode(jsontext);
       if (typeof(json.error) != 'undefined'){
           $('sendProduct').set('disabled', '');
           $('sendProduct').getElements('span')[1].set('text','确定');
           //显示发货失败消息
           if (json.msg.delivery_bn){
        	   $('error').show().set('html',json.msg.delivery_bn + '发货单发货失败!');
           }           
       }else{
           $('sendProduct').set('disabled', 'true');
       }

    }
});
</script>